﻿

function BtnGoto_OnClick()
{
	try
	{
		var parentElem = self; //top.frames["main"];
		var BaseForm = self.document.forms[0]; //top.frames["main"].document.forms[0];
		var PageIndex = 1;
		var IsOver = false;
	    if(isNaN(BaseForm.PageIndex.value) || BaseForm.PageIndex.value==0)
		{
			 ShowError("頁數請輸入數字且須大於 '0' ");
			 return;
		}
		if (parentElem.XMLExecSchema != undefined) {
			if(BaseForm.PageIndex.value!=""){
			   PageIndex = (parseInt(BaseForm.PageIndex.value) - 1) * parseInt(BaseForm.PageIndex.lang);
			   RowCount = BaseForm.PageIndex.rowcount;
			   IsOver = RowCount < PageIndex;
			}   
			if(!IsOver){
				var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,"",PageIndex);
				//執行語法
				DataBind(XmlDocument);
			}
			else
			{
				ShowError("超出資料總頁次，請重新輸入頁次");
				BaseForm.PageIndex.select();
			}
		}
		else {
			throw new Error('無法取得資料內容,請先搜尋資料 !');
		}
	}
	catch(e)
	{
		ShowError(e.message);
	}
}
/*
	事件功能: 導入第一頁

	傳入值 : null
	傳出值 : null
*/
function BtnFirst_OnClick()
{
	try
	{
		var parentElem = self; //top.frames["main"];
		var BaseForm = self.document.forms[0]; //top.frames["main"].document.forms[0];
		if (parentElem.XMLExecSchema != undefined) {
			var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,"", BaseForm.BtnFirst.lang);
			//執行語法
			DataBind(XmlDocument);
		}
		else {
			throw new Error('無法取得資料內容,請先搜尋資料 !');
		}
	}
	catch(e)
	{
		ShowError(e.message);
	}
}
/*
	事件功能: 導入前一頁

	傳入值 : null
	傳出值 : null
*/
function BtnPrev_OnClick()
{
	try
	{
		var parentElem = self; //top.frames["main"];	
		var BaseForm = self.document.forms[0]; //top.frames["main"].document.forms[0];
		if (parentElem.XMLExecSchema != undefined) {
			var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,"", BaseForm.BtnPrev.lang);
			//執行語法
			DataBind(XmlDocument);
		}
		else {
			throw new Error('無法取得資料內容,請先搜尋資料 !');
		}
	}
	catch(e)
	{
		ShowError(e.message);
	}
}
function BtnLast_OnClick()
{
	try
	{
		var parentElem =self; //top.frames["main"];
		var BaseForm = self.document.forms[0]; //top.frames["main"].document.forms[0];
		if (parentElem.XMLExecSchema != undefined) {
			var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,"", BaseForm.BtnLast.lang);
			//執行語法
			DataBind(XmlDocument);
		}
		else {
			throw new Error('無法取得資料內容,請先搜尋資料 !');
		}
	}
	catch(e)
	{
		ShowError(e.message);
	}
}
function BtnNext_OnClick()
{
	try
	{
		var parentElem = self; //top.frames["main"];
		var BaseForm =self.document.forms[0]; //top.frames["main"].document.forms[0];
		if (parentElem.XMLExecSchema != undefined) {
			var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,"", BaseForm.BtnNext.lang);
			//執行語法
			DataBind(XmlDocument);
		}
		else {
			throw new Error('無法取得資料內容,請先搜尋資料 !');
		}
	}
	catch(e)
	{
		ShowError(e.message);
	}
}
function gridBar_MouseOver()
{
	var e=window.event.srcElement;
	if(e.tagName=="TD" && e.id!="")
	{
		e.className ='barover';
	}
}
function gridBar_MouseOnClick()
{
	var sSort = null;
	var e = window.event.srcElement;
	if(e.id == undefined || e.id == "") {
		return;
	}
	try
	{
		sSort = e.id;
		e.className = "barover";
		if(e.childNodes.length >1)
		{
			 if (e.lastChild.src.indexOf("bar_up") != -1)
				sSort = e.id + " DESC";
		}
		var parentElem = self; //top.frames["main"];
		//執行語法
		var XmlDocument = ExecuteQuery(parentElem.XMLExecSchema.innerHTML,sSort, "0");
		DataBind(XmlDocument);
	}
	catch(e)
	{
		ShowError("gridBar_MouseOnClick() :" + e.message +'\n 無法取得資料內容,請重新查詢 !');
	}
}
function gridBar_MouseOut()
{
	var e=window.event.srcElement;
	if(e.tagName=="TD")
	{
		e.className ="header";
	}
}

/*
	功能: 取得user選擇之rowIndex
	傳入值 : 無

	傳出值 : index
*/
function getSelectedIndex() 
{
	var intSelected = 0;   // 判斷有無選擇資料暫存變數

	for(var i = 0; i < self.DataGrid.gridBodyTable.rows.length; i++ ) 
	{
		var classname =self.DataGrid.gridBodyTable.rows[i].cells[0].className.toString();
		if (classname.trim() == "gridSelectOn")
		{
			intSelected++; // 有選資料則+1
			return i;
		}
	}
	
	// 判斷是否有選擇資料

	if (intSelected == 0)
	{
		return -1;	
	}
	
}

/*
	功能: 依定義的 key 值, 取得查詢字串
	傳入值 : 資料列序號 (DataList.rowIndex)
	傳出值 : 查詢字串
*/
function getSingleQueryString(p_index) 
{
	// 拆解使用者選取的資料key值 (index,name)
    var QueryString = "";
    //Ken : WTF!
	var searchKeys = document.getElementById("searchKey").detailSet.split(",");

	// 組合查詢的語法

	for (var i = 1; i < searchKeys.length; i++) 
	{
		QueryString += self.gridBar.rows[0].cells[searchKeys[i]].id + "=" 
		+ escape(self.DataGrid.gridBodyTable.rows[parseInt(p_index)].cells[searchKeys[i]].innerText) + "&";
	}
	
	if (QueryString.length > 0) 
	{
		return QueryString.substring(0, QueryString.length - 1);
	}
	else 
	{
		throw "query data invalid!";
	}
}